package cn.wolfcode.service.impl;

import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.domain.AccountTransaction;
import cn.wolfcode.domain.OperateIntergralVo;
import cn.wolfcode.mapper.AccountTransactionMapper;
import cn.wolfcode.mapper.UsableIntegralMapper;
import cn.wolfcode.service.IUsableIntegralService;
import cn.wolfcode.web.msg.IntergralCodeMsg;
import com.alibaba.fastjson.JSON;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.util.Date;

/**
 * Created by lanxw
 */
@Service
public class UsableIntegralServiceImpl implements IUsableIntegralService {
    @Autowired
    private UsableIntegralMapper usableIntegralMapper;
    @Autowired
    private AccountTransactionMapper accountTransactionMapper;

    @Override
    public void decrIntergral(OperateIntergralVo vo) {
        int effectCount= usableIntegralMapper.decrIntergral(vo.getUserId(),vo.getValue());
        if (effectCount==0){
            throw new BusinessException(IntergralCodeMsg.INTERGRAL_NOT_ENOUGH);
        }
    }

    @Override
    public void incrIntegral(OperateIntergralVo vo) {
        usableIntegralMapper.incrIntergral(vo.getUserId(),vo.getValue());
    }

    @Override
    public void decrIntergralTry(OperateIntergralVo vo, BusinessActionContext context) {
        System.out.println("try");
//        插入事务控制表
        AccountTransaction log=new AccountTransaction();
        log.setTxId(context.getXid());
        log.setActionId(context.getBranchId());
        Date now=new Date();
        log.setGmtCreated(now);
        log.setGmtModified(now);
        log.setUserId(vo.getUserId());
        log.setAmount(vo.getValue());
        accountTransactionMapper.insert(log);
//        执行业务逻辑
        int effectCount = usableIntegralMapper.decrIntergral(vo.getUserId(), vo.getValue());
        if (effectCount==0){
            throw new BusinessException(IntergralCodeMsg.INTERGRAL_NOT_ENOUGH);
        }
    }

    @Override
    public void decrIntergralConfirm(BusinessActionContext context) {
        System.out.println("confirm");
        AccountTransaction accountTransaction = accountTransactionMapper.get(context.getXid(), context.getBranchId());
        if (accountTransaction != null) {
//            如果不为空
            if (accountTransaction.STATE_TRY==accountTransaction.getState()){
//                状态如果为try执行commit逻辑
//                执行日志的状态，空操作
                accountTransactionMapper.updateAccountTransactionState(context.getXid(),context.getBranchId(),AccountTransaction.STATE_COMMIT,AccountTransaction.STATE_TRY);
            }else  if (AccountTransaction.STATE_COMMIT==accountTransaction.getState()){
//                如果是commit状态不做事情
            }else {
//                如果为其他的写mq通知管理员
            }
        }else {
//            mq通知管理员
        }
    }

    @Override
    public void decrIntergralRollback(BusinessActionContext context) {
        System.out.println("rollback");
        AccountTransaction accountTransaction = accountTransactionMapper.get(context.getXid(), context.getBranchId());
        if (accountTransaction != null) {
            if (AccountTransaction.STATE_TRY==accountTransaction.getState()){
//                处于try状态,将状态修改为cancel状态
                accountTransactionMapper.updateAccountTransactionState(context.getXid(),context.getBranchId(),AccountTransaction.STATE_CANCEL,AccountTransaction.STATE_TRY);
//                执行cancel业务逻辑,添加积分
                usableIntegralMapper.incrIntergral(accountTransaction.getUserId(),accountTransaction.getAmount());
            }else if (AccountTransaction.STATE_CANCEL==accountTransaction.getState()){
//                之前一句执行力cancl,幂等处理
            }
            else {
//                其他情况,通知管理员
            }
        }else {
            OperateIntergralVo vo = JSON.parseObject((String) context.getActionContext("vo"), OperateIntergralVo.class);
//         不存在日志记录,插入事务控制表
            AccountTransaction log=new AccountTransaction();
            log.setTxId(context.getXid());
            log.setActionId(context.getBranchId());
            Date now=new Date();
            log.setGmtCreated(now);
            log.setGmtModified(now);
            log.setUserId(vo.getUserId());
            log.setAmount(vo.getValue());
            accountTransactionMapper.insert(log);
        }
    }


}
